//
//  XDLocalPush.m
//  iPadAuditAuthorise
//
//  Created by XiangDongMBP on 16/9/14.
//  Copyright © 2016年 XiangDong. All rights reserved.
//

#import "XDLocalPush.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

@implementation XDLocalPush

+ (void)registerLocalPush {
    // iOS10
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        // 使用 UNUserNotificationCenter 来管理通知
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        //监听回调事件
//        center.delegate = self;
        
        //iOS 10 使用以下方法注册，才能得到授权
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound)
                              completionHandler:^(BOOL granted, NSError * _Nullable error) {
                                  // Enable or disable features based on authorization.
                              }];
        
        //获取当前的通知设置，UNNotificationSettings 是只读对象，不能直接修改，只能通过以下方法获取
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            
        }];
    }else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0){
        //iOS8以上要先获取权限
        UIApplication * application = [UIApplication sharedApplication];
        if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
            UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
            UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
            [application registerUserNotificationSettings:settings];
        }
    }
}

+ (void)prsentLocalPush:(NSDictionary *)dic {
    UILocalNotification * localNoti = [[UILocalNotification alloc] init];
//    localNoti.fireDate = date;    // 设置本地通知的触发时间（如果要立即触发，无需设置）
//    localNoti.timeZone = [NSTimeZone defaultTimeZone];    // 设置本地通知的时区
    localNoti.repeatInterval = 0;   //不重复
    localNoti.alertBody = @"本地推送内容是xxx";
    localNoti.soundName = UILocalNotificationDefaultSoundName;// 设置提醒的声音，可以自己添加声音文件，这里设置为默认提示声
    localNoti.applicationIconBadgeNumber++; //icon上面的小标记
    localNoti.userInfo = dic;
    
    [[UIApplication sharedApplication] presentLocalNotificationNow:localNoti];
}

//使用 UNNotification 本地通知
+(void)registerNotification:(NSInteger )alerTime{
    
    // 使用 UNUserNotificationCenter 来管理通知
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    
    //需创建一个包含待通知内容的 UNMutableNotificationContent 对象，注意不是 UNNotificationContent ,此对象为不可变对象。
    UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
    content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];
    content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body" arguments:nil];
    content.sound = [UNNotificationSound defaultSound];
    content.userInfo = @{};
    
    // 在 alertTime 后推送本地推送
//    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
//                                                  triggerWithTimeInterval:alerTime repeats:NO];
//    UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
//                                                                          content:content trigger:trigger];
    UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
                                                                          content:content trigger:nil];
    
    //添加推送成功后的处理！
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"本地通知" message:@"成功添加推送" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//        [alert addAction:cancelAction];
//        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
    }];
}

@end
